import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '@/store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    directer:'home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
    {
    path: '/user',
      name: 'User',
      component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
      redirect:'/user/cart',
      children: [
        {path:'cart',component: () => import('../components/user/Cart.vue')}
      ]
    },
    {
      path: '/order',
      name: 'Order',
      component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue')
    },
    {
      path: '/free',
      name: 'Free',
      component: () => import(/* webpackChunkName: "free" */ '../views/Free.vue')
  },
  {
    path: '/details',
    name: 'Details',
    component: () => import(/* webpackChunkName: "details" */ '../views/Details.vue')
  },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/Goods.vue')
},
  {
    path: '*',
    name: 'error',
    component: () => import(/* webpackChunkName: "error" */ '../components/Error.vue')
  }
  
  // {
  //   path: '/about',
  //   name: 'about',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  mode: "history",
  base:process.env.BASE_URL,
  routes
})
// 全局前置守卫
// router.beforeEach((to, from, next) => {
//   // 每一次路由改变时,都会执行该钩子函数
//   //判断条件是去到user组件的时候
//   if (to.path == '/user') {
//     let token = localStorage.getItem('x-auth-token')
//     if (token) {
//       next()
//     } else {
//       //提示登录
//       store.dispatch('showToast/asyncIsShowToast', {
//         show: true,
//         msg: '请先登录',
//         type:'danger'
//       })
//     }
//   } else {
//     next()//其它组件正常显示
//   }
// })
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
